﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using PagedList;
using WebSiteFool.Models;

namespace WebSiteFool.Controllers
{
    public class HomeController : BaseController
    {
        private const int _pageSize = 5;
        public ActionResult Index()
        {
            var article = DbWorker.ArticleRepository.Get(orderBy: OrderBy).FirstOrDefault();
            if (article != null)
            {
                var tempArticle = PreviewFormatter(article, false);
                return View(tempArticle);
            }
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult LatestArticles()
        {
            var articles = DbWorker.ArticleRepository.Get(orderBy: OrderBy).Take(10);
            return PartialView(articles);
        }

        public ActionResult AllArticles(int? page)
        {
            if (!page.HasValue)
            {
                page = 1;
            }
            var articles = DbWorker.ArticleRepository.Get(orderBy: OrderBy, includeProperties: "Comments, Visits");
            IList<Article> newArticles = new List<Article>();
            foreach (var item in articles)
            {
                newArticles.Add(PreviewFormatter(item));
            }
            return View(newArticles.ToPagedList(page.Value, _pageSize));
        }

        public ActionResult Error()
        {
            return View();
        }

        private static IOrderedQueryable<Article> OrderBy(IQueryable<Article> articles)
        {
            return articles.OrderByDescending(m => m.CreateDate);
        }

        private static Article PreviewFormatter(Article article, bool stringCut = true)
        {
            var newArticle = new Article()
            {
                Id = article.Id,
                Title = article.Title,
                CreateDate = article.CreateDate,
                UpdateDate = article.UpdateDate,
                Content = article.Content,
                Comments = article.Comments,
                Visits = article.Visits
            };
            int imgStart = 0;
            while (imgStart > 0)
            {
                imgStart = newArticle.Content.IndexOf("<img", 0);
                if (imgStart > 0)
                {
                    var imgEnd = newArticle.Content.IndexOf(">", imgStart);
                    newArticle.Content = newArticle.Content.Remove(imgStart, imgEnd - imgStart + 1);
                }
            }
            if (newArticle.Content.Length > 500 && stringCut)
            {
                newArticle.Content = newArticle.Content.Substring(0, 300);
            }
            return newArticle;
        }
    }
}
